package edu.unh.sustain;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.ExpandableListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;
import android.widget.Spinner;
import android.widget.TextView;

/**
 * Displays route 4A of Wildcat Transit as an expandable list.  This means
 * that each stop will appear as a top level "group", and after it is touched
 * it will expand to show the different times that the bus will service
 * that stop.
 * 
 * Displays schedule for route 4A on weekdays, weekends, and reduced service.
 * @author Blake Janelle
 *
 */
public class Route4bActivity extends ExpandableListActivity {
	
	String timeframe;
	Context ct = this;
	int tabNum = 1;
	int spinnerState = 0;
	HashMap<String,Integer> runs = new HashMap<String,Integer>();
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    
	    timeframe = getTimeframe(savedInstanceState);
	    ArrayAdapter<CharSequence> adapter = null;
	    this.addStringRuns();
	    setContentView(R.layout.route4ab);
	    
	    // set up the spinner that controls the current timeframe
	    adapter = ArrayAdapter.createFromResource(
	            this, R.array.r3_timeframes, android.R.layout.simple_spinner_item);
	    Spinner spinner = (Spinner) findViewById(R.id.timeframes);
	    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    spinner.setAdapter(adapter);
	    spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
	    
	    // set up an expandable list that groups every stop, and when pressed, expands to display each stop time
	    SimpleExpandableListAdapter lstAdapter =
				new SimpleExpandableListAdapter
				(
					this,
					createGroupList(), 						// Creating group List.
					R.layout.expand_group_row,				// Group item layout XML.
					new String[] { "Group Item" },			// the key of group item.
					new int[] { R.id.row },				// ID of each group item.-Data under the key goes into this TextView.
					createChildList(),						// childData describes second-level entries.
					R.layout.expand_child_row,				// Layout for sub-level entries(second level).
					new String[] {"Sub Item"},				// Keys in childData maps to display.
					new int[] { R.id.grp_child }			// Data under the keys above go into these TextViews.
				);
					
	    ExpandableListView a = (ExpandableListView)findViewById(android.R.id.list);
	    a.setAdapter(lstAdapter);
		
	    // set text box labels for the corresponding timeframe
	    if( timeframe.equals("weekends"))
	    {
	    	spinnerState = 1;
	    	TextView title = (TextView)findViewById(R.id.textView1);
	    	title.setText(R.string.b4w_title);
	    	TextView route = (TextView)findViewById(R.id.textView2);
	    	route.setText(R.string.b4w_route);
	    	TextView range = (TextView)findViewById(R.id.textView3);
	    	range.setText(R.string.b4w_range);
		    spinner.setSelection(spinnerState);
	    }
	    else if( timeframe.equals("reduced"))
	    {
	    	spinnerState = 2;
	    	TextView title = (TextView)findViewById(R.id.textView1);
	    	title.setText(R.string.b4r_title);
	    	TextView route = (TextView)findViewById(R.id.textView2);
	    	route.setText(R.string.b4r_route);
	    	TextView range = (TextView)findViewById(R.id.textView3);
	    	range.setText(R.string.b4r_range);
		    spinner.setSelection(spinnerState);
	    }
	    else
	    {
	    	TextView title = (TextView)findViewById(R.id.textView1);
	    	title.setText(R.string.b4_title);
	    	TextView route = (TextView)findViewById(R.id.textView2);
	    	route.setText(R.string.b4_route);
	    	TextView range = (TextView)findViewById(R.id.textView3);
	    	range.setText(R.string.b4_range);
		    spinner.setSelection(spinnerState);
	    }
	}
	
	/**
	 * Create list of stops which are the "groups" of the expandable list
	 * 
	 * @return List<HashMap<String,String>>
	 */
	private List<HashMap<String,String>> createGroupList()
	{
		String[] stops;
				
		if(timeframe.equals("weekdays"))
			stops = getResources().getStringArray(R.array.b4stops);
		else if(timeframe.equals("weekends"))
			stops = getResources().getStringArray(R.array.b4wstops);
		else
			stops = getResources().getStringArray(R.array.b4rstops);
		
		ArrayList<HashMap<String,String>> result = new ArrayList<HashMap<String,String>>();
		  
		for( int i = 0 ; i < stops.length; i++ )
		{
			HashMap<String,String> m = new HashMap<String,String>();
			m.put( "Group Item", stops[i] );
			result.add( m );
		}
		
		return (List<HashMap<String,String>>)result;
    }

	/**
	 * Create list of times for each stop.
	 * 
	 * @return List<ArrayList<HashMap<String,String>>>
	 */
	private List<ArrayList<HashMap<String,String>>> createChildList()
	{
		String[] stops;
		String prefix;
		
		if(timeframe.equals("weekdays"))
		{
			stops = getResources().getStringArray(R.array.b4stops);
			prefix = "b4stop";
		}
		else if(timeframe.equals("weekends"))
		{
			stops = getResources().getStringArray(R.array.b4wstops);
			prefix = "b4wstop";
		}
		else
		{
			stops = getResources().getStringArray(R.array.b4rstops);
			prefix = "b4rstop";
		}
		
		ArrayList<ArrayList<HashMap<String,String>>> result = new ArrayList<ArrayList<HashMap<String,String>>>();
		
		for( int i = 0 ; i < stops.length; i++ )
		{
			String index = prefix + (i+1);
			String[] run = getResources().getStringArray(runs.get(index));
			ArrayList<HashMap<String,String>> secList = new ArrayList<HashMap<String,String>>();
			
			for( int n = 0 ; n < run.length ; n++ )
			{
				HashMap<String,String> child = new HashMap<String,String>();
				child.put( "Sub Item", run[n] );
				secList.add( child );
			}
			
			result.add( secList );
		}
		return result;
	}
	
	/**
	 * Extracts the timeframe data from the Instance and returns it
	 * 
	 * @param savedInstanceState previous state
	 * @return the string or null
	 */
	private String getTimeframe(Bundle savedInstanceState)
	{
		String ret = null;
		
		if( savedInstanceState != null )
	    {
	    	ret = (String) savedInstanceState.getString("timeframe");
	    }
	    else
	    {
	    	Bundle extras = getIntent().getExtras();
	    	
	    	if( extras != null )
	    		ret = extras.getString("timeframe");
	    }
		
		return ret;
	}
	
	/**
	 * Returns the Sting equivalent of the spinner position
	 * @param current spinner position
	 * @return String equivalent
	 */
	private String getPos(int current)
	{
		if(current == 0)
			return new String("weekdays");
		else if(current == 1)
			return new String("weekends");
		else
			return new String("reduced");
	}
	
	/**
	 * Saves the state of the screen.  This is called on orientation changes. 
	 */
	public void onSaveInstanceState(Bundle savedInstanceState)
	{
		  savedInstanceState.putString("timeframe", timeframe);
		  super.onSaveInstanceState(savedInstanceState);
	}
	/**
	 * A "run array" is a string array of times when each
	 * stop is serviced.  Each run array id (references route3.xml)
	 * is added to the runs HashMap so that these array ids can 
	 * be referenced as strings in createGroupList() and 
	 * createChildList().
	 */
	private void addStringRuns()
	{
		// weekday stops
		runs.put( "b4stop1", R.array.b4stop1);
	    runs.put( "b4stop2", R.array.b4stop2);
	    runs.put( "b4stop3", R.array.b4stop3);
	    runs.put( "b4stop4", R.array.b4stop4);
	    runs.put( "b4stop5", R.array.b4stop5);
	    runs.put( "b4stop6", R.array.b4stop6);
	    runs.put( "b4stop7", R.array.b4stop7);
	    runs.put( "b4stop8", R.array.b4stop8);
	    runs.put( "b4stop9", R.array.b4stop9);
	    runs.put( "b4stop10", R.array.b4stop10);
	    runs.put( "b4stop11", R.array.b4stop11);
	    runs.put( "b4stop12", R.array.b4stop12);
	    runs.put( "b4stop13", R.array.b4stop13);
	    runs.put( "b4stop14", R.array.b4stop14);
	    runs.put( "b4stop15", R.array.b4stop15);
	    runs.put( "b4stop16", R.array.b4stop16);
	    runs.put( "b4stop17", R.array.b4stop17);
	    runs.put( "b4stop18", R.array.b4stop18);
	    runs.put( "b4stop19", R.array.b4stop19);
	    runs.put( "b4stop20", R.array.b4stop20);
	    runs.put( "b4stop21", R.array.b4stop21);
	    runs.put( "b4stop22", R.array.b4stop22);
	    runs.put( "b4stop23", R.array.b4stop23);
	    runs.put( "b4stop24", R.array.b4stop24);
	    runs.put( "b4stop25", R.array.b4stop25);
	    runs.put( "b4stop26", R.array.b4stop26);
	    runs.put( "b4stop27", R.array.b4stop27);
	    runs.put( "b4stop28", R.array.b4stop28);
	    runs.put( "b4stop29", R.array.b4stop29);
	    runs.put( "b4stop30", R.array.b4stop30);
	    runs.put( "b4stop31", R.array.b4stop31);
	    runs.put( "b4stop32", R.array.b4stop32);
	    runs.put( "b4stop33", R.array.b4stop33);
	    runs.put( "b4stop34", R.array.b4stop34);
	    runs.put( "b4stop35", R.array.b4stop35);
	    runs.put( "b4stop36", R.array.b4stop36);
	    runs.put( "b4stop37", R.array.b4stop37);
	    runs.put( "b4stop38", R.array.b4stop38);
	    runs.put( "b4stop39", R.array.b4stop39);
	    runs.put( "b4stop40", R.array.b4stop40);
	    runs.put( "b4stop41", R.array.b4stop41);
	    runs.put( "b4stop42", R.array.b4stop42);
	    runs.put( "b4stop43", R.array.b4stop43);
	    runs.put( "b4stop44", R.array.b4stop44);
	    
	    // weekend stops
	    runs.put( "b4wstop1", R.array.b4wstop1);
	    runs.put( "b4wstop2", R.array.b4wstop2);
	    runs.put( "b4wstop3", R.array.b4wstop3);
	    runs.put( "b4wstop4", R.array.b4wstop4);
	    runs.put( "b4wstop5", R.array.b4wstop5);
	    runs.put( "b4wstop6", R.array.b4wstop6);
	    runs.put( "b4wstop7", R.array.b4wstop7);
	    runs.put( "b4wstop8", R.array.b4wstop8);
	    runs.put( "b4wstop9", R.array.b4wstop9);
	    runs.put( "b4wstop10", R.array.b4wstop10);
	    runs.put( "b4wstop11", R.array.b4wstop11);
	    runs.put( "b4wstop12", R.array.b4wstop12);
	    runs.put( "b4wstop13", R.array.b4wstop13);
	    runs.put( "b4wstop14", R.array.b4wstop14);
	    runs.put( "b4wstop15", R.array.b4wstop15);
	    runs.put( "b4wstop16", R.array.b4wstop16);
	    runs.put( "b4wstop17", R.array.b4wstop17);
	    runs.put( "b4wstop18", R.array.b4wstop18);
	    runs.put( "b4wstop19", R.array.b4wstop19);
	    runs.put( "b4wstop20", R.array.b4wstop20);
	    runs.put( "b4wstop21", R.array.b4wstop21);
	    runs.put( "b4wstop22", R.array.b4wstop22);
	    runs.put( "b4wstop23", R.array.b4wstop23);
	    runs.put( "b4wstop24", R.array.b4wstop24);
	    runs.put( "b4wstop25", R.array.b4wstop25);
	    runs.put( "b4wstop26", R.array.b4wstop26);
	    runs.put( "b4wstop27", R.array.b4wstop27);
	    runs.put( "b4wstop28", R.array.b4wstop28);
	    runs.put( "b4wstop29", R.array.b4wstop29);
	    runs.put( "b4wstop30", R.array.b4wstop30);
	    runs.put( "b4wstop31", R.array.b4wstop31);
	    runs.put( "b4wstop32", R.array.b4wstop32);
	    runs.put( "b4wstop33", R.array.b4wstop33);
	    runs.put( "b4wstop34", R.array.b4wstop34);
	    runs.put( "b4wstop35", R.array.b4wstop35);
	    runs.put( "b4wstop36", R.array.b4wstop36);
	    runs.put( "b4wstop37", R.array.b4wstop37);
	    runs.put( "b4wstop38", R.array.b4wstop38);
	    runs.put( "b4wstop39", R.array.b4wstop39);
	    runs.put( "b4wstop40", R.array.b4wstop40);
	    runs.put( "b4wstop41", R.array.b4wstop41);
	    runs.put( "b4wstop42", R.array.b4wstop42);
	    runs.put( "b4wstop43", R.array.b4wstop43);
	    runs.put( "b4wstop44", R.array.b4wstop44);
	    
	    // reduced service stops
	    runs.put( "b4rstop1", R.array.b4rstop1);
	    runs.put( "b4rstop2", R.array.b4rstop2);
	    runs.put( "b4rstop3", R.array.b4rstop3);
	    runs.put( "b4rstop4", R.array.b4rstop4);
	    runs.put( "b4rstop5", R.array.b4rstop5);
	    runs.put( "b4rstop6", R.array.b4rstop6);
	    runs.put( "b4rstop7", R.array.b4rstop7);
	    runs.put( "b4rstop8", R.array.b4rstop8);
	    runs.put( "b4rstop9", R.array.b4rstop9);
	    runs.put( "b4rstop10", R.array.b4rstop10);
	    runs.put( "b4rstop11", R.array.b4rstop11);
	    runs.put( "b4rstop12", R.array.b4rstop12);
	    runs.put( "b4rstop13", R.array.b4rstop13);
	    runs.put( "b4rstop14", R.array.b4rstop14);
	    runs.put( "b4rstop15", R.array.b4rstop15);
	    runs.put( "b4rstop16", R.array.b4rstop16);
	    runs.put( "b4rstop17", R.array.b4rstop17);
	    runs.put( "b4rstop18", R.array.b4rstop18);
	    runs.put( "b4rstop19", R.array.b4rstop19);
	    runs.put( "b4rstop20", R.array.b4rstop20);
	    runs.put( "b4rstop21", R.array.b4rstop21);
	    runs.put( "b4rstop22", R.array.b4rstop22);
	    runs.put( "b4rstop23", R.array.b4rstop23);
	    runs.put( "b4rstop24", R.array.b4rstop24);
	    runs.put( "b4rstop25", R.array.b4rstop25);
	    runs.put( "b4rstop26", R.array.b4rstop26);
	    runs.put( "b4rstop27", R.array.b4rstop27);
	    runs.put( "b4rstop28", R.array.b4rstop28);
	    runs.put( "b4rstop29", R.array.b4rstop29);
	    runs.put( "b4rstop30", R.array.b4rstop30);
	    runs.put( "b4rstop31", R.array.b4rstop31);
	    runs.put( "b4rstop32", R.array.b4rstop32);
	    runs.put( "b4rstop33", R.array.b4rstop33);
	    runs.put( "b4rstop34", R.array.b4rstop34);
	    runs.put( "b4rstop35", R.array.b4rstop35);
	    runs.put( "b4rstop36", R.array.b4rstop36);
	    runs.put( "b4rstop37", R.array.b4rstop37);
	    runs.put( "b4rstop38", R.array.b4rstop38);
	    runs.put( "b4rstop39", R.array.b4rstop39);
	    runs.put( "b4rstop40", R.array.b4rstop40);
	    runs.put( "b4rstop41", R.array.b4rstop41);
	    runs.put( "b4rstop42", R.array.b4rstop42);
	    runs.put( "b4rstop43", R.array.b4rstop43);
	    runs.put( "b4rstop44", R.array.b4rstop44);
	}
	
	/**
	 * If the spinner item that was selected is different from the current
	 * spinner page, then it goes to the appropriate page.
	 *
	 */
	public class MyOnItemSelectedListener implements OnItemSelectedListener
	{		
	    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
	    	Intent i;
		    
	    	if( pos != spinnerState )
	    	{
	    		i = new Intent();
	    		i.setClass( ct, Route4Activity.class);
	    		i.putExtra("timeframe", getPos(pos));
	    		i.putExtra("tab", tabNum);
	            startActivityForResult(i, 0);
	    	}
	    }

	    public void onNothingSelected(AdapterView<?> parent) {
	    	// Do nothing.
	    }
	}
}
